package com.yeyks.controller.common;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.em.SmsCodeTypeEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.TextValidator;
import com.yeyks.commonReference.append.sms.SmsService;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "短信接口")
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("短信接口 --> 发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "0登录注册验证码，1提现验证码，2绑定账户验证码，4推广海报验证码", required = true, dataType = "int")
    })
    @PostMapping("/code")
    public AjaxResponseBody code(@RequestParam(name = "phone") String phone,
                                 @RequestParam(name = "type") Integer type) {
        checkParamPhone(phone);
        Arguments.intIsNull(type, "验证码类型必传");
        if (SmsCodeTypeEnum.PROMOTION_POSTER.value().equals(type)) {
            UserInfo userInfo = userInfoService.selectUseInfoPhone(phone);
            Arguments.beanIsNull(userInfo, "该用户未注册");
        }
        smsService.sendSmsCode(phone, type);
        return AjaxResponseBody.success(true);
    }

    @ApiOperation(value = "短信接口 --> 验证发送的注册短信是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "验证码", dataType = "String")
    })
    @RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
    public AjaxResponseBody verifyCode(@RequestParam(name = "phone") String phone,
                                       @RequestParam(name = "code") String code) {
        checkParamPhone(phone);
        if (Arguments.strIsNull(code)) {
            throw new ServiceException("验证码输入有误");
        }
        if (!smsService.verifySmsForSignup(phone, code)) {
            return AjaxResponseBody.failed("验证码输入有误");
        }
        return AjaxResponseBody.success(true);
    }

    @ApiOperation(value = "短信接口 --> 海报验证发送的注册短信是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "验证码", dataType = "String")
    })
    @RequestMapping(value = "/poster/verifyCode", method = RequestMethod.POST)
    public AjaxResponseBody posterVerifyCode(@RequestParam(name = "phone") String phone,
                                             @RequestParam(name = "code") String code) {
        checkParamPhone(phone);
        UserInfo userInfo = userInfoService.selectUseInfoPhone(phone);
        Arguments.beanIsNull(userInfo, "该用户未注册");
        Arguments.isTrue(StrUtil.isBlank(code), "验证码不能为空");
        if (!smsService.verifySmsForSignup(phone, code)) {
            return AjaxResponseBody.failed("验证码输入有误");
        }
        return AjaxResponseBody.success(ImmutableMap.of("userId", userInfo.getId()));
    }

    private void checkParamPhone(String phone) {
        if (Arguments.strIsNull(phone)) {
            throw new ServiceException("手机号不能为空");
        }
        if (!TextValidator.MOBILE.boolCheck(phone)) {
            throw new ServiceException("手机号格式非法");
        }
    }

}
